#include "header.h"


// 客户端
int main()
{
    // 1. 创建套接字
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    // 2. 服务器结构体
    struct sockaddr_in svr;
    svr.sin_family = AF_INET;
    svr.sin_port = ntohs(44444);
    svr.sin_addr.s_addr = inet_addr("82.157.169.207");


    // 3. 和服务器发送连接请求
    if (connect(sock, (const struct sockaddr*) &svr, sizeof svr) != 0)
    {
        cout << "客户端发送请求连接失败" << endl;
        exit(1);
    }

    // 4. 和服务器开始通信
    char sendmsg[1024] = {0};
    while (true)
    {
        cout << "enter: ";
        fflush(stdout);
        fgets(sendmsg, 1023, stdin);
        // 然后发送数据
        write(sock, sendmsg, strlen(sendmsg));
        cout << "成功发送数据" << endl;

        char recvmsg[1024] = {0};
        int rd = read(sock, recvmsg, 1023);
        if (rd > 0) 
        {
            recvmsg[rd] = 0;
            cout << "收到服务器发回的数据：" << recvmsg << endl;
        }
    }
}